package com.bwie.parkdemo.bind;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.bwie.lib_core.bind.rvadapter.GridVerticalAdapterEntity;
import com.bwie.parkdemo.R;
import com.bwie.parkdemo.adapter.AddPunchAdapter;
import com.bwie.parkdemo.adapter.CarImgAdapter;
import com.bwie.parkdemo.adapter.ColorAdapter;
import com.bwie.parkdemo.adapter.DepartmentUserAdapter;
import com.bwie.parkdemo.adapter.HistorySignAdapter;
import com.bwie.parkdemo.adapter.ParkingAllAdapter;
import com.bwie.parkdemo.adapter.PatrolesAdapter;
import com.bwie.parkdemo.adapter.PointAdapter;
import com.bwie.parkdemo.adapter.RepairAdapter;
import com.bwie.parkdemo.adapter.RepairUserAdapter;
import com.bwie.parkdemo.adapter.SelAllApplyAdapter;
import com.bwie.parkdemo.adapter.VisitorAdapter;
import com.bwie.parkdemo.databinding.LayoutPatrolFootBinding;
import com.bwie.parkdemo.databinding.LayoutPatrolHeadBinding;
import com.bwie.parkdemo.mvvm.viewmodel.AddPatrolViewModel;
import com.bwie.parkdemo.widget.dividerdtemdecoration.ParkDividerItemDecoration;
import com.bwie.parkdemo.widget.dividerdtemdecoration.PointDividerItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yanyusong.y_divideritemdecoration.Y_DividerItemDecoration;
import java.util.List;

public class BindRVAdapter {
    //自定义全部车位列表recyclerview
    @BindingAdapter(value = {"gridvertical","divider","listener"},requireAll = false)
    public static void bindRecycler(RecyclerView recyclerView,
                                    GridVerticalAdapterEntity entity,
                                    int divider,
                                    BaseQuickAdapter.OnItemClickListener listener){
        //获取与recyclerview关联的适配器->null第一次进入;!null数据改变进入
        BaseQuickAdapter oldadapter = (BaseQuickAdapter) recyclerView.getAdapter();
        if (oldadapter == null){
            if (entity.getType() == 0){
                GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),entity.getSpanCount());
                manager.setOrientation(entity.getOrientation());
                recyclerView.setLayoutManager(manager);
                //构建分隔线
                recyclerView.addItemDecoration(createDivider(recyclerView.getContext(),
                        divider ));//添加分隔线
                oldadapter = new ParkingAllAdapter(entity.getLayoutId(),entity.getDatas());
                recyclerView.setAdapter(oldadapter);
                oldadapter.setOnItemClickListener(listener);
            }
        }else {
            oldadapter.setNewData(entity.getDatas());
        }
    }

    //自定义线性横向recyclerview
    //listener监听器
    //type适配器类型
    //datas数据源
    @BindingAdapter(value = {"listener","type","datas","childlister"},requireAll = false)
    public static void HorizontalRV(RecyclerView recyclerView,
                                    BaseQuickAdapter.OnItemClickListener listener,
                                    int type, List<Object>datas,
                                    BaseQuickAdapter.OnItemChildClickListener childClickListener){
        BaseQuickAdapter adapter = (BaseQuickAdapter) recyclerView.getAdapter();
        if (adapter==null){
            //分隔线构建添加，布局管理器构建添加
            LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recyclerView.setLayoutManager(manager);
            //根据type类型构建适配器
            if (type == ColorAdapter.TYPE){
                //颜色适配器
                adapter = new ColorAdapter();
            }
            if (type == CarImgAdapter.TYPE){
                adapter = new CarImgAdapter();
            }
            if (type == RepairUserAdapter.TYPE)
                adapter = new RepairUserAdapter();
            adapter.setOnItemClickListener(listener);
            //适配器添加监听器
            recyclerView.setAdapter(adapter);
        }
        //复用adapter
        adapter.setNewData(datas);
    }

    //垂直线性recyclerview自定义属性
    //监听器
    //type
    //数据源
    @BindingAdapter(value = {"datas","type","itemlistener","childitemlistener"},requireAll = false)
    public static void VerticalRv(RecyclerView recyclerView, List<Object>datas,
                                  int type,BaseQuickAdapter.OnItemClickListener itemlistener,
                                  BaseQuickAdapter.OnItemChildClickListener childClickListener){
        BaseQuickAdapter adapter = (BaseQuickAdapter) recyclerView.getAdapter();
        //第一次进入需要构建适配器
        if (adapter == null){
            LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);
            if (type == VisitorAdapter.TYPE){
                adapter = new VisitorAdapter();
            }
            if (type == DepartmentUserAdapter.type)
                adapter = new DepartmentUserAdapter();
            if (type == RepairAdapter.type)
                adapter = new RepairAdapter();
            if (type == PatrolesAdapter.TYPE)
                adapter = new PatrolesAdapter();
            if (type == AddPunchAdapter.TYPE)
                adapter = new AddPunchAdapter();
            if (type == SelAllApplyAdapter.TYPE)
                adapter = new SelAllApplyAdapter();
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(itemlistener);
            if (childClickListener!=null) {
                adapter.setOnItemChildClickListener(childClickListener);
                Log.e("ZXWQ","setOnItemClickListener1");
            }
        }
        adapter.setNewData(datas);
    }


    //垂直5列线性布局
    @BindingAdapter(value = {"signdatas"},requireAll = false)
    public static void GridVerticalRV(RecyclerView recyclerView,
                                      List<Object>signdatas){
        BaseQuickAdapter adapter = (BaseQuickAdapter) recyclerView.getAdapter();
        if (adapter == null){
            View lineVidew = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.line_chart_top,null);
//            lineVidew.setLayoutParams(
//                    new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
//                            1));
            LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
            manager.setOrientation(GridLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);
            adapter = new HistorySignAdapter();
            adapter.addHeaderView(lineVidew);
            recyclerView.setAdapter(adapter);
        }
        adapter.setNewData(signdatas);
    }

    //具有头布局以及脚布局效果的网格样式的recyclerview
    //datas->网格显示的数据源
    //griditemlistener->item点击事件
    //headlayout->头布局layoutid
    //footlayout->脚布局layoutid
    @BindingAdapter(value = {"datas","griditemlistener","headlayout","footlayout","vm"},requireAll = false)
    public static void GridRecyclerView(RecyclerView recyclerView, List<Object>datas,
                                        BaseQuickAdapter.OnItemClickListener griditemlistener, int headlayout,
                                        int footlayout, AddPatrolViewModel viewModel){
        BaseQuickAdapter adapter = (BaseQuickAdapter) recyclerView.getAdapter();
        if (adapter == null){
            //第一次进入
            //获取头布局
            LayoutPatrolHeadBinding headBinding = DataBindingUtil.inflate(LayoutInflater.from(recyclerView.getContext()),headlayout,null,false);
            View headView = headBinding.getRoot();
            headBinding.setVm(viewModel);
            headBinding.setClick(viewModel.clickListener);
            //获取脚布局
            LayoutPatrolFootBinding footBinding = DataBindingUtil.inflate(LayoutInflater.from(recyclerView.getContext()),footlayout,null,false);
            View footView = footBinding.getRoot();
            footBinding.setClick(viewModel.clickListener);
            //添加布局管理器
            GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),3);
            manager.setOrientation(GridLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(manager);
            //添加分割线
            recyclerView.addItemDecoration(createDivider(recyclerView.getContext()));
            //创建适配器对象
            adapter = new PointAdapter();
            //添加头布局
            adapter.addHeaderView(headView);
            //添加脚布局
            adapter.addFooterView(footView);
            //设置点击监听器
            adapter.setOnItemClickListener(griditemlistener);
            //适配器设置到recyclerview
            recyclerView.setAdapter(adapter);
        }
        adapter.setNewData(datas);

    }
    //构建分隔线方法
    private static Y_DividerItemDecoration createDivider(Context ctx,int type){
        Y_DividerItemDecoration divider = null;
        if (type == ParkDividerItemDecoration.ID)
            divider = new ParkDividerItemDecoration(ctx);
        return divider;
    }

    //构建巡逻点的分割线方法
    private static PointDividerItemDecoration createDivider(Context ctx){
        PointDividerItemDecoration divider = new PointDividerItemDecoration(ctx);
        return divider;
    }

}
